package cn.boysoft.web.client;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * @author 付厚俊
 * CreateTime 2013-4-29 下午4:22:52
 */
public class JsonClient {
private String url="http://localhost:9090";
	@SuppressWarnings("deprecation")
	public String send(String json) throws HttpException, IOException{
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		URI uri = new URI(url);
		PostMethod method = new PostMethod();
		method.setURI(uri);
		method.setRequestBody(json);
		httpClient.executeMethod(method);
		String response = method.getResponseBodyAsString();
		return response;
	}
	
	public byte[] send(InputStream in) throws HttpException, IOException{
		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		URI uri = new URI(url);
		PostMethod method = new PostMethod();
		method.setURI(uri);
		method.setRequestBody(in);
		httpClient.executeMethod(method);
		byte[] response = method.getResponseBody();
		return response;
	}
}
